-
-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expand info/debug/warning msgs if USB device serial_number
not readable
#423
base: main
Are you sure you want to change the base?
Conversation
…readable if INSTR USB device, - say that device is USB INSTR that failed in warning message - catch exception - debug message with traceback if logging level is DEBUG - info message 1-line error summary if logging level is INFO - if path f"/dev/bus/usb/{dev.bus:03d}/{dev.address:03d}" exists, check permissions on that file & refer to FAQ link (known issue on Linux) if RAW USB device, - say that device is USB RAW that failed in warning message
Examples of messages running the code import logging
import pyvisa
logging.basicConfig(level=logging.DEBUG)
rm = pyvisa.ResourceManager("@py")
rm.list_resources() on Linux with permissions /dev/usb/001/016 not set correctly (I also have a USB RAW device on my system, so you can see the modified message for that as well).
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for tackling this! Left some notes, will test the code in a bit.
Please note I do not have authority over the code, @MatthieuDartiailh has the last word. My comments are suggestions at best.
@@ -292,6 +295,39 @@ def list_resources() -> List[str]: | |||
"usb_interface_number": intfc, | |||
}, | |||
) | |||
logging_level = logger.getEffectiveLevel() | |||
if logging_level <= logging.DEBUG: | |||
if exc_strs := traceback.format_exception(err): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the benefit of doing this over logger.debug("error while reading serial number", exc_info=err)
?
I am also not sure if this amount of pre-processing is worth it. On DEBUG
I'd prefer to have all exceptions logged in general. For higher levels, we can just add the exception message to the warning's dict maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unaware of that kwarg to logger methods! Will change to using exc_info
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @arr-ee here. Please update the code accordingly.
exc_strs[0].strip(), | ||
) | ||
|
||
dev_path = f"/dev/bus/usb/{dev.bus:03d}/{dev.address:03d}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is linux-specific and should be gated as such (see https://github.com/pyvisa/pyvisa/blob/main/pyvisa/util.py#L1005 for example).
maybe also move the check logic into a helper method in usbutil
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As to your second point, particularly if we are going to implement this for both USB INSTR and USB RAW devices, then it certainly makes sense to move the check logic into a separate module. But I can do that anyway even just to keep the code here cleaner, omitting the gory details that are only pertinent to one OS.
As to your first comment, I thought about gating based on platform. (I also tried to figure out if there's an equivalent for FreeBSD/OS X, but failed to find that.) But I figured by the time I went through all the logic required to check that the sys.platform
was something this check could work on, I could do it the Python-ic (duck-typing) way and simply look for the file's existence, check the file's permissions, and go from there, and that would essentially take care of "does this check work on my particular platform?" issues.
I'm certainly not married to that way, I get why one might not want to use the duck-type perspective for this check. This almost certainly only works on Linux & nowhere else, and maybe we discover a Linux where it doesn't work, and then we would just want to add to the gating condition. So, yeah, I'll put the check within a if sys.platform.startswith("linux"): ...
check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I can do that anyway even just to keep the code here cleaner, omitting the gory details that are only pertinent to one OS.
Yup, plus it will be easier to test (that's a tricky thing though).
But I figured by the time I went through all the logic required to check that the sys.platform was something this check could work on, I could do it the Python-ic (duck-typing) way and simply look for the file's existence, check the file's permissions, and go from there, and that would essentially take care of "does this check work on my particular platform?" issues.
This is fair! I prefer an explicit check to make the intent explicit to the readers: "this is only for linux", but my disregard for pythonic way of doing things can and should be questioned :)
I also tried to figure out if there's an equivalent for FreeBSD/OS X, but failed to find that.
Yeah it's very system-specific, and I don't think we'd have permissions issue on macos (we'd have other ones!). I was wondering if there's a "better" way to get to the device file on linux as well, but it seems that string magic should do the trick until proven otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here also I think an explicit check will make the intent clearer for people looking at it.
@@ -336,7 +372,7 @@ def list_resources() -> List[str]: | |||
serial = dev.serial_number | |||
except (NotImplementedError, ValueError, usb.USBError): | |||
msg = ( | |||
"Found a device whose serial number cannot be read." | |||
"Found a USB RAW device whose serial number cannot be read." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we run into the same permissions issue here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do. I thought pyvisa didn't care about RAW devices (because it doesn't control them), but I realize now that assumption may be incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
USB RAW are weird because there are supported by NI but are not part of the IVI specs. If we can provide more information we should though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I observed in pyvisa issue Linux host and getting permissions to talk to a USBTMC device
USBTMC devices show up as both /dev/usbtmcN and /dev/bus/usb/NNN/NNN
Both instances need to have suitable permissions for pyvisa with pyvisa-py to be able to communicate
If only the /dev/usbtmcN has permissions then you get WARNING Found a device whose serial number cannot be read
logger.warning( | ||
"User does not have permission to %s %s, so the above USB INSTR" | ||
" device cannot be used by pyvisa; see" | ||
" https://pyvisa.readthedocs.io/projects/pyvisa-py/en/latest/faq.html" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should link to the specific anchor once docs are updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only issue with that is we need to keep the two in sync; a change in the docs will necessitate a change in the code. That could be annoying for a maintainer, so I left it out. But if that's what you want, I'm happy add the anchor to the link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt that docs organisation will change drastically enough to make this a big issue, and since we're going to the trouble of linking we might as well link to the exact spot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree a direct link would be better. Anything that can reduce the number of obscure bug reports is something I am happy to include.
Really appreciate all your helpful feedback, @arr-ee . I will push updates according to your comments ASAP, and hopefully have a first pass at adding to the docs that can be reviewed as well. I want to re-iterate how helpful the feedback you gave is, not really being aware of all the ins-and-outs of pyvisa & pyvisa-py (like code organization in the usbutils module; wouldn't have thought of that as an outsider). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Once the minor requested changes are in I will merge.
@@ -292,6 +295,39 @@ def list_resources() -> List[str]: | |||
"usb_interface_number": intfc, | |||
}, | |||
) | |||
logging_level = logger.getEffectiveLevel() | |||
if logging_level <= logging.DEBUG: | |||
if exc_strs := traceback.format_exception(err): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @arr-ee here. Please update the code accordingly.
exc_strs[0].strip(), | ||
) | ||
|
||
dev_path = f"/dev/bus/usb/{dev.bus:03d}/{dev.address:03d}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here also I think an explicit check will make the intent clearer for people looking at it.
logger.warning( | ||
"User does not have permission to %s %s, so the above USB INSTR" | ||
" device cannot be used by pyvisa; see" | ||
" https://pyvisa.readthedocs.io/projects/pyvisa-py/en/latest/faq.html" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree a direct link would be better. Anything that can reduce the number of obscure bug reports is something I am happy to include.
@@ -336,7 +372,7 @@ def list_resources() -> List[str]: | |||
serial = dev.serial_number | |||
except (NotImplementedError, ValueError, usb.USBError): | |||
msg = ( | |||
"Found a device whose serial number cannot be read." | |||
"Found a USB RAW device whose serial number cannot be read." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
USB RAW are weird because there are supported by NI but are not part of the IVI specs. If we can provide more information we should though.
@jllanfranchi any idea when you will be able to address the comments ? |
Friendly ping @jllanfranchi |
if INSTR USB device,
if RAW USB device,
I will work on documentation fixes now, but wanted to get this in front of you for feedback in the meantime, @arr-ee (et al.).
black . && isort -c . && flake8
withnoerrors black & isort OK; flake8 doesn't like a few lines' lengths